
// pages/myOrder/index.js
const app = getApp()
const req = require('../../utils/req')
const util = require('../../utils/util')
var timer
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:1,
    page:1,
    limit:10,
    lists:[],
    empty:false,
    keywords:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    if(options.type){
      this.setData({
        type:parseInt(options.type)
      })
    }
    this.getList()
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.data.isShow){
      this.setData({
        lists:[],
        page:1,
        empty:false,
        isLoadingAll:false
  
      })
      this.getList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(!this.data.isLoadingAll){
        this.data.page++
        this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  tab:function(e){
    var type=e.currentTarget.dataset.type
    this.setData({
      type:type,
      lists:[],
      page:1,
      empty:false,
      isLoadingAll:false,
      keywords:''
    })
    clearInterval(timer)
    this.getList()

  },
  getList:function(){
    var that=this
    var data={}
    data.type=this.data.type
    data.limit=this.data.limit
    data.page=this.data.page
    if(this.data.keywords){
      data.keywords=this.data.keywords
    }
    clearInterval(timer)
    app.getList('/ht/user/userOrderList',data,'POST',that)
  },
  input:function(e){
    console.log(e)
    var key=e.currentTarget.dataset.name
    this.setData({
      [key]:e.detail.value
    })
  },
  submit:function(e){
    console.log(e)
    console.log('keywords',e.detail.value.keywords,'--',e.detail.value)
    var keywords=e.detail.value.keywords || e.detail.value
    console.log(keywords)
   if(keywords){
    this.setData({
      keywords:keywords,
      lists:[],
      page:1,
      empty:false,
      isLoadingAll:false,
    })
    this.getList()
   }else{
      wx.showToast({
        title: '请输入关键词',
        icon:'none',
        duration:1500
      })
   }
  },
  countDown:function(){
    
    var that = this
    var lists=this.data.lists
    timer = setInterval(function () {
      lists.map(item=>{
          
          if(item.surplusTime>0){
            item.surplusTime=parseInt(item.surplusTime)-1
            if(item.surplusTime==0){
              clearInterval(timer)
              that.setData({
                lists:[],
                p:1,
                empty:false,
                isLoadingAll:false,
              })
             
              that.getList()
            }
            item.ms=util.timeStampToHms(item.surplusTime)
          }
      })
      //console.log(lists)
      that.setData({
        lists:lists
      })
      
    }, 1000);
},
cancelOrder:function(e){
  var that=this
  var id=e.currentTarget.dataset.id
  var lists=this.data.lists
  var data={}
  data.id=id
  wx.showModal({
    title: '温馨提示',
    content: '是否确认取消该订单',
    success: function (res) {
      if (res.confirm) {
        req.request('/ht/user/delOrder',data,'POST').then(res=>{
          var jsonArray = lists.filter(item => item.id !== id)
          console.log(jsonArray)
          clearInterval(timer)
          that.setData({
            lists:jsonArray
          })
          that.countDown()
        })
      } else if (res.cancel) {
        console.log('取消删除')
      }
    }
  })
  
},
toPay:function(e){
  var item=e.currentTarget.dataset.item
  wx.navigateTo({
    url: '/pages/submitDetail/index?id='+item.id+'&type='+item.type,
  })
},
toDetail:function(e){
  var item=e.currentTarget.dataset.item
  wx.navigateTo({
    url: '/pages/myOrder/detail?id='+item.id+'&type='+item.type,
  })
},
refund:function(e){
  var that=this
  var id=e.currentTarget.dataset.id
  var data={}
  data.orderId=id
  wx.showModal({
    title: '温馨提示',
    content: '是否确认申请退款',
    success: function (res) {
      if (res.confirm) {
        req.request('/ht/user/orderRefund',data,'POST').then(res=>{
          console.log(res)
          that.onShow()
        })
      } else if (res.cancel) {
        console.log('取消退款')
      }
    }
  })
  
},

})